//
//  KADetailHeaderView.m
//  KeepAccount
//
//  Created by Veer on 2017/12/19.
//  Copyright © 2017年 Veer. All rights reserved.
//

#import "KADetailHeaderView.h"


@interface KADetailHeaderView()

@property (nonatomic, weak) UILabel *yearLbl;
@property (nonatomic, weak) UILabel *monthLbl;
@property (nonatomic, weak) UILabel *incomeLbl;
@property (nonatomic, weak) UILabel *expenditureLbl;

@end


@implementation KADetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    UILabel *yearLbl = [[UILabel alloc] init];
    yearLbl.text = @"2017年";
    yearLbl.font = VRFont(14);
    yearLbl.textColor = RGB(122, 107, 60);
    [self addSubview:yearLbl];
    self.yearLbl = yearLbl;
    [yearLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.height.mas_equalTo(30);
    }];
    
    UIView *monthView = [[UIView alloc] init];
    [self addSubview:monthView];
    [monthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(yearLbl.mas_bottom);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(93);
        make.height.mas_equalTo(30);
    }];
    
    // 月
    UILabel *monthLbl = [[UILabel alloc] init];
    monthLbl.text = @"12";
    self.monthLbl = monthLbl;
    monthLbl.font = VRFont(26);
    monthLbl.textColor = RGB(51, 51, 51);
    [monthView addSubview:monthLbl];
    [monthLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(20);
    }];
    
    UILabel *monthTitleLbl = [[UILabel alloc] init];
    monthTitleLbl.text = @"月";
    monthTitleLbl.font = VRFont(14);
    monthTitleLbl.textColor = RGB(51, 51, 51);
    [monthView addSubview:monthTitleLbl];
    [monthTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(monthLbl).offset(-4);
        make.left.mas_equalTo(monthLbl.mas_right).offset(2);
    }];
    
    // 下拉图
    UIImageView *dropImgView = [[UIImageView alloc] init];
    dropImgView.image = [UIImage imageNamed:@"pull_down_selector_normal"];
    [self addSubview:dropImgView];
    [dropImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(monthTitleLbl).offset(2);
        make.left.mas_equalTo(monthTitleLbl.mas_right).offset(8);
        make.width.mas_equalTo(11);
        make.height.mas_equalTo(11);
    }];
    
    // 竖线
    UIView *vLineView = [[UIView alloc] init];
    vLineView.backgroundColor = RGB(51, 51, 51);
    [self addSubview:vLineView];
    [vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(monthView);
        make.left.mas_equalTo(monthView.mas_right);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(monthView).offset(-4);
    }];
    
    // 收入 income
    UILabel *incomeTitleLbl = [[UILabel alloc] init];
    incomeTitleLbl.text = @"收入";
    incomeTitleLbl.font = VRFont(14);
    incomeTitleLbl.textColor = RGB(122, 107, 60);
    [self addSubview:incomeTitleLbl];
    [incomeTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(yearLbl);
        make.left.mas_equalTo(monthView.mas_right).offset(30);
    }];
    
    UILabel *incomeLbl = [[UILabel alloc] init];
    incomeLbl.text = @"91.00";
    incomeLbl.font = VRFont(20);
    incomeLbl.textColor = RGB(51, 51, 51);
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",incomeLbl.text]];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(incomeLbl.text.length - 3, 3)];
    incomeLbl.attributedText = attrStr;
    [self addSubview:incomeLbl];
    self.incomeLbl = incomeLbl;
    [incomeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(monthView).offset(0);
        make.left.mas_equalTo(monthView.mas_right).offset(30);
    }];
    
    // 支出 expenditure
    CGFloat moneyW = (SCREEN_WIDTH - 93 - 30) * 0.5;
    
    UILabel *expenditureTitleLbl = [[UILabel alloc] init];
    expenditureTitleLbl.text = @"支出";
    expenditureTitleLbl.font = VRFont(14);
    expenditureTitleLbl.textColor = RGB(122, 107, 60);
    [self addSubview:expenditureTitleLbl];
    [expenditureTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(yearLbl);
        make.left.mas_equalTo(monthView.mas_right).offset(30 + moneyW);
    }];
    
    UILabel *expenditureLbl = [[UILabel alloc] init];
    expenditureLbl.text = @"91.00";
    expenditureLbl.font = VRFont(20);
    expenditureLbl.textColor = RGB(51, 51, 51);
    NSMutableAttributedString *expenditureLblAttrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",incomeLbl.text]];
    [expenditureLblAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(incomeLbl.text.length - 3, 3)];
    expenditureLbl.attributedText = expenditureLblAttrStr;
    [self addSubview:expenditureLbl];
    self.expenditureLbl = expenditureLbl;
    [expenditureLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(monthView).offset(0);
        make.left.mas_equalTo(monthView.mas_right).offset(30 + moneyW);
    }];
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [monthView addGestureRecognizer:tapGesture];
}


#pragma mark - action
- (void)handleTapGesture:(UITapGestureRecognizer *)recongnizer
{
    if (self.choseMonthBlock) {
        self.choseMonthBlock();
    }
}

#pragma mark setter & getter
- (void)setYear:(NSInteger)year
{
    _year = year;
    
    self.yearLbl.text = [NSString stringWithFormat:@"%ld年", (long)year];
}

- (void)setMonth:(NSInteger)month
{
    _month = month;
    
    self.monthLbl.text = [NSString stringWithFormat:@"%ld", (long)month];
}

- (void)setIncome:(double)income
{
    _income = income;
    
    NSMutableAttributedString *expenditureLblAttrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f", income]];
    [expenditureLblAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(expenditureLblAttrStr.length - 3, 3)];
    self.incomeLbl.attributedText = expenditureLblAttrStr;
}

- (void)setExpenditure:(double)expenditure
{
    _expenditure = expenditure;
    
    NSMutableAttributedString *expenditureLblAttrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f", expenditure]];
    [expenditureLblAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(expenditureLblAttrStr.length - 3, 3)];
    self.expenditureLbl.attributedText = expenditureLblAttrStr;
}


@end
